(function($){
	//iframe式tab 只在最上层html统一调度 
	//初始化 生成 根据浏览器屏幕宽度 N个空tab
	yanhx.addFunc("closeTab",function(){
		yanhx.exec("tabHandle.closeTab",this.parent());
	});
	yanhx.addFunc("openTab",function(){
		yanhx.find("tabHandle").openObjTab.call(this);
	});
	yanhx.addComponent("tabHandle",{
		opt:null,
		tabs:{},
		i:1,
		old_h:0,
		tabTemplate:'<div class="tabTitle"><span class="text"></span><a class="ctrl" href="javascript:void(0)" callFunc="click:closeTab"></a></div>',
		frameTemplate:'<div class="tabItem" align="center"><iframe class="index_frame" src="" frameborder="0"></iframe></div>',
		menuCfg:[
		  {name:"关闭",call:"closeSrc"},            
		  {name:"刷新",call:"flush"},
		  {name:"关闭其他标签",call:"closeOther"},
		  {name:"关闭所有标签",call:"closeAll"},
		  {name:"关闭右侧标签",call:"closeRight"},
		  {name:"关闭左侧标签",call:"closeLeft"}
		],
		closeSrc:function(){
			this.closeTab(yanhx.exec("contextHandle.getSrc"));
		},
		closeLeft:function(){
			var src=yanhx.exec("contextHandle.getSrc");
			var subs=src.prevAll("div");
			var len=subs.length;
			for(var i=0;i<len;i++){
				this.closeTabOnly($(subs[i]));
			}
			src.fire("click");
		},
		closeRight:function(){
			var src=yanhx.exec("contextHandle.getSrc");
			var subs=src.nextAll("div");
			var len=subs.length;
			for(var i=0;i<len;i++){
				this.closeTabOnly($(subs[i]));
			}
			src.fire("click");
		},
		closeAll:function(){
			var src=yanhx.exec("contextHandle.getSrc");
			var subs=src.parent().children("div");
			var len=subs.length;
			for(var i=0;i<len;i++){
				this.closeTabOnly($(subs[i]));
			}
		},
		closeOther:function(){
			var src=yanhx.exec("contextHandle.getSrc");
			var subs=src.parent().children("div");
			var len=subs.length;
			for(var i=0;i<len;i++){
				var t=$(subs[i]);
				if(t.attr("tabuqkey")==src.attr("tabuqkey")){
					continue;
				}else{
					this.closeTabOnly(t);
				}
			}
			this.openFirst();
		},
		flush:function(){
			var src=yanhx.exec("contextHandle.getSrc");
			var cfg=this.getTabByElem(src);
			var frame=cfg.content.find("iframe");
			frame.attr("src",frame.attr("src"));
		},
		openObjTab:function(){
			if(!this.hasClass("active")){
				yanhx.exec("tabHandle.hideCurTab");
				yanhx.exec("tabHandle.activeTab",this);
			}
		},
		getMenu:function(){
			if(this.menuTemplate) return this.menuTemplate;
			else{
				this.menuTemplate="";
				var len=this.menuCfg.length;
				for(var i=0;i<len;i++){
					this.menuTemplate+="<li callFunc='click:tabHandle."+this.menuCfg[i].call
					+"'>"+this.menuCfg[i].name+"</li>";
				}
				return this.menuTemplate;
			}
		},
		openMenu:function(src){
			yanhx.exec("contextHandle.show",{src:src,elem:this.getMenu()});
		},
		hideCurTab:function(){
			var elem=this.opt.tool.children("div.active");
			if(!elem.length)
				return;
			var tab=this.getTabByElem(elem);
			tab.tab.removeClass("active");
			tab.content.hide();
		},
		activeTab:function(elem){
			var tab=this.getTabByElem(elem);
			tab.tab.addClass("active");
			tab.content.show();
		},
		getTabByElem:function(elem){
			return this.tabs[elem.attr("tabUqKey")];
		},
		open:function(bean){
			//
			var old=this.opt.tool.children("#"+bean.tabKey);
			if(old.length){
				this.openObjTab.call(old);
				return;
			}
			this.hideCurTab();
			//没有则 新建tab
			var empty=this.findEmpty();
			if(!empty){
				return;
			}
			empty.tab.children(".text").text(bean.tabName).attr("title",bean.tabName);
			empty.tab.attr("id",bean.tabKey);
			empty.tab.addClass("active");
			empty.tab.addClass("idle").bind("mouseover",this.showCtrl)
			.bind("mouseout",this.hideCtrl);
			empty.content.children("iframe").attr("src",bean.tabSrc).css("height",this.opt.frameHeight);
			empty.tab.appendTo(this.opt.tool).show();
			empty.content.appendTo(this.opt.content).show();
			empty.empty=false;
			//保存参数
			empty.options=bean;
			return empty;
		},
		findEmpty:function(){
			for(var item in this.tabs){
				if(this.tabs[item].empty){
					return this.tabs[item];
				}
			}
			yanhx.exec("myWin.alert",{
				content:'超出最大tab数:'+this.opt.max
			});
			return null;
		},
		init:function(opt){
			this.opt=opt;
			//有传MAX 则创建空的frame 占位
			if(opt.max){
				for(var i=0;i<opt.max;i++){
					this.createFrame(true);
				}
			}
			this.opt.frameHeight=$(document).height()-this.opt.content.offset().top;
			//this.opt.frameHeight=this.opt.frameHeight<700?700:this.opt.frameHeight;
			this.opt.content.offset().top;
			var t=this;
		},
		createFrame:function(isEmpty){
			var tab=$(this.tabTemplate);
			var content=$(this.frameTemplate);
			var key="tab_uq_"+parseInt(Math.random()*10000)+this.i++;
			//置为空闲  使用连接池机制   
			tab.attr("tabUqKey",key).attr("callFunc","click:openTab menu:tabHandle.openMenu;noBubble");
			content.hide();
			this.tabs[key]={
				tab:tab,
				content:content,
				empty:isEmpty,
				options:null
			};
		},
		showCtrl:function(e){
			$(this).children("a").show();
		},
		hideCtrl:function(e){
			$(this).children("a").hide();
		},
		closeTabOnly:function(obj){
			yanhx.exec("noBubble");
			var key=obj.attr("tabUqKey");
			var curTab=this.tabs[key];
			curTab.tab.remove();
			curTab.content.remove();
			curTab.tab.children("a").hide();
			curTab.empty=true;
			curTab.content.children("iframe").attr("src","");
			curTab.options=null;
		},
		closeTab:function(obj){
			yanhx.exec("noBubble");
			var key=obj.attr("tabUqKey");
			var curTab=this.tabs[key];
			curTab.tab.remove();
			curTab.content.remove();
			curTab.tab.children("a").hide();
			curTab.empty=true;
			curTab.content.children("iframe").attr("src","");
			if(curTab.options.openFromKey){
				this.openByKey(curTab.options.openFromKey);
			}else if(curTab.tab.hasClass("active")){
				this.openFirst();
			}
			curTab.options=null;
		},
		openByKey:function(key){
			var curTab=this.tabs[key];
			curTab.tab.addClass("active");
			curTab.content.show();
		},
		openFirst:function(){
			var tabs=this.opt.tool.children(".tabTitle");
			if(tabs.length){
				this.activeTab($(tabs.get(0)));
			}
		},
		//从当前tab 打开新的frame
		openTabFromCurTab:function(bean){
			var active=this.getActiveTab();
			if(active){
				bean.openFromKey=active.tab.attr('tabUqKey');
				bean.openFromTabKey=active.options.tabKey;
			}
			this.open(bean);
		},
		closeAndCallBack:function(data){
			var active=this.getActiveTab();
			if(active){
				var openFrom=this.tabs[active.options.openFromKey];
				//源未关闭 且 未被替换（关闭后被另个open打开）
				if(!openFrom.empty&&openFrom.options.tabKey==active.options.openFromTabKey){
					openFrom.content.children("iframe").get(0).contentWindow.yanhx.exec(active.options.callback,data);
				}
				this.closeTab(active.tab);
			}
		},
		closeCurTab:function(){
			var active=this.getActiveTab();
			if(active){
				this.closeTab(active.tab);
			}
		},
		getActiveTab:function(){
			var elem=this.opt.tool.children("div.active");
			if(!elem.length)
				return null;
			return this.getTabByElem(elem);
		},
		getPos:function(){
			var tabs=this.getActiveTab();
			var retBean=tabs.content.offset();
			return retBean;
		}
	});
	
})(jQuery);